package gcontent.config.persistents.node;

import java.util.ArrayList;
import java.util.HashMap;

import gcontent.config.error.ConfigEx;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ObjectNode {
	
	private String id = "";
	private String classname = "";
	private HashMap<String, MethodNode> method_list = new HashMap<String, MethodNode>();
	private ArrayList<String> ref = new ArrayList<String>();
	
	public String getId()
	{
		return this.id;
	}
	
	public String getClassName()
	{		
		return this.classname;
	}
	
	public MethodNode getMethodNode(String method_name)
	{
		return this.method_list.get(method_name);
	}
	
	public ArrayList<String> getReferences()
	{
		return this.ref;
	}

	public ObjectNode(Node node) throws ConfigEx
	{
		NodeList node_list;			
		NamedNodeMap node_map;
		
		node_map = node.getAttributes();
		if(node_map == null)
			throw new ConfigEx("ObjectNode : not id attribute defined");
		
		if(node_map.getNamedItem("id") == null)
			throw new ConfigEx("ObjectNode : not id attribute defined");

		this.id = node_map.getNamedItem("id").getNodeValue();
		this.classname = node_map.getNamedItem("classname").getNodeValue();
		
		if(node_map.getNamedItem("ref_id") != null)
			this.ref.add(node_map.getNamedItem("ref_id").getNodeValue());
		
		
		node_list = node.getChildNodes();
		for(int i = 0; node_list.item(i) != null; i++) {
			Node node_c;
			String name_n;
			
			node_c = node_list.item(i);
			name_n = node_c.getNodeName();

			if(name_n.equals("method")) {
				MethodNode m_node;
				
				m_node = new MethodNode(node_c);
				method_list.put(m_node.getId(), m_node);
			} else if(name_n.charAt(0) != '#')
				throw new ConfigEx("ObjectNode => unknown node " + name_n);
		}
	}

}
